package com.person.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.modules.sys.dao.SysAuditDao;
import com.person.modules.sys.entity.SysAuditEntity;
import com.person.modules.sys.entity.SysCompanyEntity;
import com.person.modules.sys.entity.SysDeptEntity;
import com.person.modules.sys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/*
@author  Edliu-630
@date    2021/3/0030 - 10:59
*/
@Service("SysAuditService")
public class SysAuditServiceImpl extends ServiceImpl<SysAuditDao, SysAuditEntity> implements SysAuditService {
    @Autowired
    SysAuditDao sysAuditDao;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Autowired
    SysCompanyService sysCompanyService;
    @Autowired
    SysDeptService sysDeptService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean audit(SysAuditEntity sysAuditEntity) {
        int count=0;
        if (sysAuditEntity.getAuditStatus()==1) {
            count = sysAuditDao.updateSysUserStatusByCompanyId(sysAuditEntity);
                    sysAuditDao.saveSysUserRole(sysAuditEntity.getCompanyId());
            SysCompanyEntity sysCompanyEntity= sysCompanyService.getById(sysAuditEntity.getCompanyId());
            SysDeptEntity sysDeptEntity=new SysDeptEntity();
            sysDeptEntity.setParentId(Long.valueOf(0));
            sysDeptEntity.setName(sysCompanyEntity.getCompanyName());
            sysDeptEntity.setOrderNum(0);
            sysDeptEntity.setDelFlag(0);
            sysDeptEntity.setCompanyId(sysCompanyEntity.getId());
            sysDeptService.save(sysDeptEntity);
        }else if (sysAuditEntity.getAuditStatus()==2){
            count =sysAuditDao.deleteSysUserByCompanyId(sysAuditEntity);
        }
        sysAuditDao.updateSysCompanyStatusByCompanyId(sysAuditEntity);
        sysAuditService.save(sysAuditEntity);
        if (count==0){
            return true;
        }else {
            return false;
        }
    }

}
